package cn.onein.edu.web.dao;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.ibatis.SqlMapClientTemplate;
import org.springframework.stereotype.Service;

import cn.onein.edu.biz.bean.LearnCategory;
import cn.onein.edu.biz.bean.LearnContent;
import cn.onein.edu.biz.bean.LearnIndex;
import cn.onein.edu.biz.bean.LearnSection;
import cn.onein.edu.biz.bean.LearnSectionDetail;

@Service
public class LearnDao {

	@Autowired
	private SqlMapClientTemplate sqlMapClientTemplate;
	
	@SuppressWarnings("unchecked")
	public List<LearnSection> getAllSection(){
		return sqlMapClientTemplate.queryForList("getAllSection");
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnCategory> getCategoryBySection(Long sectionId){
		return sqlMapClientTemplate.queryForList("getCategoryBySection", sectionId);
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnCategory> getAllCategoryBySection(Long sectionId){
		return sqlMapClientTemplate.queryForList("getCategoryBySection", sectionId);
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnContent> getLatestContentBySection(Long sectionId){
		return sqlMapClientTemplate.queryForList("getLatestContentBySection", sectionId);
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnContent> getSectionHotTopContent(String sectionName,Integer count){
		List<LearnContent> result = (List<LearnContent>)sqlMapClientTemplate.queryForList("getSectionHotTopContent", sectionName);
		
		if(result != null){
			if(count >= result.size()){
				return result;
			}
			else{
				return result.subList(0, count);
			}				
		}
		else{
			return null;
		}
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnContent> getContentGroupByCategory(long categoryId){
		return sqlMapClientTemplate.queryForList("getContentGroupByCategory", categoryId);
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnContent> getHotContentByCategory(long categoryId){
		return sqlMapClientTemplate.queryForList("getHotContentByCategory", categoryId);
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnContent> getPerpageContentByCategory(Map<String,Object> map){
		return sqlMapClientTemplate.queryForList("getPerpageContentByCategory", map);
	}
	
	public LearnSection getSectionBySectionName(String name){
		return (LearnSection)sqlMapClientTemplate.queryForObject("getSectionBySectionName",name);
	}
	
	public LearnCategory getCategoryByCategoryName(String name){
		return (LearnCategory)sqlMapClientTemplate.queryForObject("getCategoryByCategoryName",name);
	}
	
	public Integer getContentCountByCategory(Long categoryId){
		return (Integer)sqlMapClientTemplate.queryForObject("getContentCountByCategory",categoryId);
	}
	
	public Integer getALLContentCountsBySectionName(String sectionName){
		return (Integer)sqlMapClientTemplate.queryForObject("getALLContentCountsBySectionName",sectionName);
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnContent> getALLContentBySectionName(Map<String,Object> map){
		return sqlMapClientTemplate.queryForList("getALLContentBySectionName", map);
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnIndex> getLearnIndexImage(){
		return sqlMapClientTemplate.queryForList("getLearnIndexImage");
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnIndex> getLearnIndexGroup(){
		return sqlMapClientTemplate.queryForList("getLearnIndexGroup");
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnIndex> getLearnIndexContentByGroup(String groupName){
		return sqlMapClientTemplate.queryForList("getLearnIndexContentByGroup",groupName);
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnSectionDetail> getSectionImageBySectionName(String name){
		return sqlMapClientTemplate.queryForList("getSectionDetailImageBySection", name);
	}
	
	@SuppressWarnings("unchecked")
	public List<LearnSectionDetail> getSectionTextBySectionName(String name){
		return sqlMapClientTemplate.queryForList("getSectionDetailTextBySection", name);
	}
	
	public LearnSectionDetail getSectionTitleBySectionName(String name){
		return (LearnSectionDetail)sqlMapClientTemplate.queryForObject("getSectionDetailTitleBySection", name);
	}
	
}
